//
//  VisitorView.swift
//  sina
//
//  Created by 江先铨 on 15/11/10.
//  Copyright © 2015年 jxqstatic. All rights reserved.
//

import UIKit
import SnapKit


@objc protocol VisitorViewDelegate: NSObjectProtocol {  //设置代理 代理社oc 的方法


    func visitorWillLogin()
    
    optional func visitorWillRegiter()
    
}

class VisitorView: UIView {
    
    weak var  visitorDelegate: VisitorViewDelegate?  //生命一个对象的属性
    
    @objc func LoginDidBtnClick() {
    
     visitorDelegate?.visitorWillLogin()
        
    }
    
    @objc func registerBtnDidClick() {
    
    visitorDelegate?.visitorWillRegiter?() //因为注册的方法为可选项 可以实现也可以不实现
        
    }

    func setUIInfo(imageName: String?, titie: String) { //这句话是用来判断当前的页面是不是首页面  如果传入nil 的话 就说明是首页 就开始执行动画效果 如果不是就需要闯入参数的名称
        
        iconView.hidden = false  //刚开始的时候icon 图片是不需要隐藏的 第一张照片的时候需要把小房子显示起来
        
        tipLabel.text = titie
        
        if imageName != nil { //判断如果传入的图片的名字不为空
            
            circleView.image = UIImage(named: imageName!) //把旋转动画的那红图片给替换掉 换成消息图片的按钮
            
           bringSubviewToFront(circleView) //把旋转的图片前置 这样就可以在切换图片的时候让图片显示出来 不被隐藏 因为只有第一张照片需要隐藏 和遮罩 其他的图片是需要清晰的显示出来的
            
            iconView.hidden = true // 把小房子给隐藏起来  其实小房子是什么时候都会有的  值是被我们认为的给隐藏了
        } else {
        
        startAnimation() //如果传入的数据是空的话 就默认是第一张图片  开始执行动画效果
        }
        
    }
    
    private func startAnimation() {
    
    let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.repeatCount = MAXFLOAT //设置重复次数
        
        anim.toValue = 2 * M_PI
        
        anim.duration = 10 //持续的时间
        //当动画结束, 或者试图处于非活跃状态, 动画不一处图层
       anim.removedOnCompletion = false
        
        circleView.layer.addAnimation(anim, forKey: nil) //把动画添加到anim 上
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
        
    }

    
   
    
    private func setupUI() {
    
        addSubview(circleView)  //旋转的图片
        
        addSubview(backView)  //背景图片
        
        addSubview(iconView) // 头像的图片
        
        addSubview(tipLabel) //刚刚没有把标签添加到子类当中
        
        addSubview(loginBtn)  //登录按钮
        
        addSubview(registerBtn) //注册按钮
        
        
//        for v in subviews {  //要使用布局首先要使布局全部失效 如果不加这句话布局是没有用的 这句话需要在使用   addConstraint 这个苹果自带的设置约束的时候写 如果是用的框架的话 是不需要写这句话的 因为snipkit 这个框架已经帮我们给封装好了 不需要我们在手动的取改写
//        
//            v.translatesAutoresizingMaskIntoConstraints = false //让frame 布局失效
//        }
        
        
      //设置圆环的约束
//      addConstraint( NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
//      addConstraint( NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        circleView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-60)
        }
      
      //设置大头标的约束
//      addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
//       addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleView.snp_centerX)
            make.centerY.equalTo(circleView.snp_centerY)
        }
        
        //设置提示文案的约束
//        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1.0, constant: 16))
//        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
//        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50))
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleView.snp_centerX)
            make.top.equalTo(circleView.snp_bottom).offset(16)
            make.width.equalTo(224)
            make.height.equalTo(50)
            
        }
        
        //注册登录的约束
//        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1.0, constant: 0))
//        
//        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
//        
//        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
//        
//        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(35)
        }
        
        //注册按钮的约束
//        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute: .Right, multiplier: 1.0, constant: 0))
//        
//        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
//        
//        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
//        
//        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(35)
        }
        
        //设置背景视图的约束
        //位移枚举|| []
        //对视图宽高约束的数值[string: NSNumber]
//        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView":backView]))
//        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-45)-[registerBtn]", options: [], metrics: nil, views: ["backView":backView,"registerBtn":registerBtn]))
//        backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        backView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.bottom.equalTo(registerBtn.snp_bottom)
        }
        //设置背景颜色 在ui 元素中, 大多数都是灰度图 或者是纯色图
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)
        
        loginBtn.addTarget(self, action: "LoginDidBtnClick", forControlEvents: .TouchUpInside)
        
        registerBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    
    
    
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    
    private lazy var tipLabel: UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜", size: 14, color: UIColor.lightGrayColor())
//    {
//    
//        let l = UILabel()
//        
//        l.text = "关注一些人，回这里看看有什么惊喜"
//        
//        l.textAlignment = NSTextAlignment.Center
//        
//        l.font = UIFont.systemFontOfSize(14)
//        
//        l.textColor = UIColor.lightGrayColor()
//        
//        l.numberOfLines = 0
//        
//       l.sizeToFit()
//        
//        return l
//    }()
    
    private lazy var loginBtn: UIButton = UIButton(title: "登录", backImage: "common_button_white_disable", color: UIColor.lightGrayColor())
//    {
//        
//        let btn = UIButton()
//        
//        btn.setTitle("登录", forState: .Normal)
//        
//        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
//        
//        btn.setTitleColor(UIColor.lightGrayColor(), forState: .Normal)
//        
//        btn.sizeToFit()
//        
//        return btn
//        
//        }()
    
    private lazy var registerBtn: UIButton = UIButton(title: "注册", backImage: "common_button_white_disable", color: mainColor)
    
//    
//    {
//    
//    let btn = UIButton()
//        
//        btn.setTitle("注册", forState: .Normal)
//        
//        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
//        
//        btn.setTitleColor(mainColor, forState: .Normal)
//        
//        return btn
//    }()
//    
}
